library(knitr)
knitr::opts_chunk$set(cache=T, comment=NA)
par(mar = c(1, 1, 1, 1))

## ----eval=FALSE----------------------------------------------------------
install.packages('statnet')
statnet::update_statnet()
## ------------------------------------------------------------------------
library(statnet)

help("ergm-terms")

## ------------------------------------------------------------------------
sessionInfo()

## ------------------------------------------------------------------------
set.seed(0)
library("sna")
snag.eb

survey.eb <- as.matrix(Ebola_SNA)
snag.eb <- network(survey.eb)
str(snag.eb)
snag.eb$names <- c("AFD", "ADB", "AGI", "AU", "Aspen", "Care", "Conc", "DFID", "ECDC", "ECOWAS", "EEAS", "EU Eb", "EC", "EP", "GIZ", "Guinea", "Medical", "Rescue", "IOM", "Liberia", "MSF", "OCHA", "Oxfam", "Phoenix", "PiH", "Plan", "Red Cross", "Samaritan", "Save Child", "SL", "Spain", "UN SG", "UNDP", "UNFPA", "UNICEF", "UNMEER", "UNMIL", "US CDC", "USAID", "WFP", "WHO", "WB")
node.size.eb <- setNames(c(0.85, 0.793, 0.826, 0.734, 0.557, 0.678, 0.59, 1.361, 1.007, 1.238, 0.911, 1.007, 1.050, 1.25, 1.069, 1.657, 1.216, 1.18, 1.261, 1.585, 1.649, 1.204, 1.144, 0.673, 1.246, 0.811, 1.391, 1.153, 1.214, 1.486, 1.22, 1.133, 1.213, 1.216, 1.399, 1.436, 1.289, 1.544, 1.34, 1.339, 1.621, 1.247), c("AFD", "ADB", "AGI", "AU", "Aspen", "Care", "Conc", "DFID", "ECDC", "ECOWAS", "EEAS", "EU Eb", "EC", "EP", "GIZ", "Guinea", "Medical", "Rescue", "IOM", "Liberia", "MSF", "OCHA", "Oxfam", "Phoenix", "PiH", "Plan", "Red Cross", "Samaritan", "Save Child", "SL", "Spain", "UN SG", "UNDP", "UNFPA", "UNICEF", "UNMEER", "UNMIL", "US CDC", "USAID", "WFP", "WHO", "WB"))
gplot(snag.eb, usearrows = T, 
      label = snag.eb$names, 
      label.pos = 9, 
      label.cex = .35, 
      label.col = "black",
      vertex.sides = c(4, 3, 5, 3, 5, 5, 5, 4, 4, 3, 4, 4, 4, 4, 4, 3, 5, 5, 50, 3, 5, 50, 5, 7, 5, 5, 5, 5, 5, 3, 4, 50, 50, 50, 50, 50, 50, 6, 6, 50, 50, 50),
      vertex.col = c (8, 3, 5, 3, 5, 5, 5, 8, 8, 3, 8, 8, 8, 8, 8, 3, 5, 5, 50, 3, 5, 50, 5, 7, 5, 5, 5, 5, 5, 3, 8, 50, 50, 50, 50, 50, 50, 6, 6, 50, 50, 50),
      edge.lwd = 0.00001,
      vertex.cex = as.matrix(node.size.eb),
      arrowhead.cex = 0.1, 
      mode = "fruchtermanreingold")

gden(snag.eb, mode = "digraph")
degree(snag.eb, gmode = "digraph", cmode = "indegree")
degree(snag.eb, gmode = "digraph", cmode = "outdegree")
closeness(snag.eb, gmode = "digraph")
betweenness(snag.eb, gmode = "digraph")

snag.degcentraliz <- centralization(snag.eb, FUN = degree, mode = "digraph", cmode="indegree", normalize = T)
snag.degcentraliz

snag.closecentraliz <- centralization(snag.eb, FUN = closeness, mode = "digraph", normalize = T)
snag.closecentraliz

snag.betwencentraliz <- centralization(snag.eb, FUN = betweenness, mode = "digraph", normalize = T)
snag.betwencentraliz

cc.snag.eb <- clique.census(snag.eb)
cc.snag.eb

library(igraph)
snag.eb.igraph <- graph_from_edgelist(survey.eb, directed = TRUE)
snag.eb.igraph
mean_distance(snag.eb.igraph)
install.packages("remotes")
remotes::install_github("schochastics/igraphUtils", force = TRUE)
library(netUtils)
survey
core_periphery(snag.eb.igraph, method = "rk1_dc", iter = 500)

## -----------------------------------------------------------------------------

survey.ebERGM <- as.matrix(Ebola_ERGM)
snag.ebERGM <- network(survey.ebERGM)
str(snag.ebERGM)
snag.ebERGM$names <- c("AFD", "ADB", "AGI", "AU", "Aspen", "Care", "Conc", "DFID", "ECDC", "ECOWAS", "EEAS", "EU Eb", "EC", "EP", "GIZ", "Guinea", "Medical", "Rescue", "IOM", "Liberia", "MSF", "OCHA", "Oxfam", "PiH", "Plan", "Red Cross", "Samaritan", "Save Child", "SL", "Spain", "UN SG", "UNDP", "UNFPA", "UNICEF", "UNMEER", "UNMIL", "US CDC", "USAID", "WFP", "WHO", "WB")
node.size.ebERGM <- setNames(c(0.85, 0.793, 0.826, 0.734, 0.557, 0.678, 0.59, 1.361, 1.007, 1.238, 0.911, 1.007, 1.050, 1.25, 1.069, 1.657, 1.216, 1.18, 1.261, 1.585, 1.649, 1.204, 1.144, 0.673, 1.246, 0.811, 1.391, 1.153, 1.214, 1.486, 1.22, 1.133, 1.213, 1.216, 1.399, 1.436, 1.289, 1.544, 1.34, 1.339, 1.621, 1.247), c("AFD", "ADB", "AGI", "AU", "Aspen", "Care", "Conc", "DFID", "ECDC", "ECOWAS", "EEAS", "EU Eb", "EC", "EP", "GIZ", "Guinea", "Medical", "Rescue", "IOM", "Liberia", "MSF", "OCHA", "Oxfam", "Phoenix", "PiH", "Plan", "Red Cross", "Samaritan", "Save Child", "SL", "Spain", "UN SG", "UNDP", "UNFPA", "UNICEF", "UNMEER", "UNMIL", "US CDC", "USAID", "WFP", "WHO", "WB"))
gplot(snag.ebERGM, usearrows = T, 
      label = snag.eb$names, 
      label.pos = 9, 
      label.cex = .35, 
      label.col = "black",
      vertex.sides = c(4, 3, 5, 3, 5, 5, 5, 4, 4, 3, 4, 4, 4, 4, 4, 3, 5, 5, 50, 3, 5, 50, 5, 7, 5, 5, 5, 5, 5, 3, 4, 50, 50, 50, 50, 50, 50, 6, 6, 50, 50, 50),
      vertex.col = c (8, 3, 5, 3, 5, 5, 5, 8, 8, 3, 8, 8, 8, 8, 8, 3, 5, 5, 50, 3, 5, 50, 5, 7, 5, 5, 5, 5, 5, 3, 8, 50, 50, 50, 50, 50, 50, 6, 6, 50, 50, 50),
      edge.lwd = 0.00001,
      vertex.cex = as.matrix(node.size.eb),
      arrowhead.cex = 0.1, 
      mode = "fruchtermanreingold")

set.seed(0)
set.vertex.attribute(snag.ebERGM, "orgGov", value = c("Gov", "IO", "NGO", "Gov", "NGO", "NGO", "NGO", "Gov", "Gov", "Gov", "Gov", "Gov", "Gov", "Gov", "Gov", "Gov", "NGO", "NGO", "IO", "Gov", "NGO", "IO", "NGO", "NGO", "NGO", "NGO", "NGO", "NGO", "Gov", "Gov", "IO", "IO", "IO", "IO", "IO", "IO", "Gov", "Gov", "IO", "IO", "IO"))
par(mfrow=c(2,2))

snagmodel.1 <- ergm(snag.ebERGM ~edges + mutual + dgwesp (0.5, fixed = TRUE) 
                    + nodeifactor ('orgGov')
                    + nodeofactor ('orgGov'))
summary(snagmodel.1)

mcmc.diagnostics(snagmodel.1)
snagmodel.1.gof <- gof(snagmodel.1)
plot(snagmodel.1.gof)

snagmodel.2 <- ergm(snag.ebERGM ~edges + mutual + dgwesp (0.5, fixed = TRUE) 
                    + nodemix ('orgGov', base = c(2)))
summary(snagmodel.2)

mcmc.diagnostics(snagmodel.2)
snagmodel.2.gof <- gof(snagmodel.2)
plot(snagmodel.2.gof)